package com.liao.controller;

import com.liao.bean.Role;
import com.liao.bean.result.PageResult;
import com.liao.bean.result.Result;
import com.liao.bean.User;
import com.liao.bean.result.Code;
import com.liao.bean.result.Msg;
import com.liao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toIndex")
    public String toIndex() throws Exception {
        System.out.println(userService.selectUserList());
        return "index";
    }

    @ResponseBody
    @RequestMapping("/login")
    public Result login(String username, String password, HttpSession session) throws Exception {
        User inputUser = new User();
        inputUser.setAccount(username);
        inputUser.setPassword(password);
        //进行登录业务
        User existUser = userService.login(inputUser);
        if (null == existUser){
            return new Result(false, Code.LOGIN_FAIL, Msg.LOGIN_FAIL);
        }else {
            //登录成功，保留用户信息
            session.setAttribute("existUser",existUser);
            return new Result(true, Code.LOGIN_SUCCESS, Msg.LOGIN_SUCCESS);
        }

    }

    @ResponseBody
    @RequestMapping("/queryUserListByPage")
    public PageResult<User> queryUserListByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                                @RequestParam(defaultValue = "5") Integer pageSize,
                                                @RequestParam(defaultValue = "") String userName) throws Exception {
        PageResult<User> userPageResult = userService.queryRoleListByPage(currentPage,pageSize,userName);

        return userPageResult;

    }

    @ResponseBody
    @RequestMapping("/addUser")
    public Result addUser(@RequestBody User user) throws Exception {
        userService.addUser(user);
        return new Result(true,Code.USER_ADD_SUCCESS,Msg.USER_ADD_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/editUser")
    public Result editUser(String oldUserId,String userId,String userName,String account,String password) throws Exception {
        userService.editUser(oldUserId,userId,userName,account,password);
        return new Result(true,Code.USER_EDIT_SUCCESS,Msg.USER_EDIT_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/queryUserList")
    public Result queryUserList() throws Exception {
        List<Role> userList=userService.queryUserList();
        return new Result(true,Code.USER_QUERY_SUCCESS,Msg.USER_QUERY_SUCCESS,userList);
    }

    @ResponseBody
    @RequestMapping("/deleteUser")
    public Result deleteUser(String userId) throws Exception {
        userService.deleteUser(userId);
        return new Result(true,Code.USER_DELETE_SUCCESS, Msg.USER_DELETE_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/deleteUserIds")
    public Result deleteRoleIds(@RequestParam("userIds[]") List<String> userIds) throws Exception {
        for (String userId : userIds) {
            try {
                userService.deleteUser(userId);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,Code.USER_DELETE_FAIL, Msg.USER_DELETE_FAIL);
            }

        }
        return new Result(true,Code.USER_DELETE_SUCCESS, Msg.USER_DELETE_SUCCESS);
    }
}
